#include "connection_handler.h"
#include "connection_manager.h"

#include <unistd.h>
#include <sys/socket.h>

void* TConnectionHandler::Run(void* self)
{
    TConnectionHandler* handler =   static_cast<TConnectionHandler*>(self);
    handler->Loop();
    return NULL;
}

void TConnectionHandler::Loop()
{
    int connectionId    =   0;
    while (TConnectionManager::KStopSignal != connectionId) {
        connectionId    =   Connections.GetUnservedConection();

        if (TConnectionManager::KStopSignal == connectionId) {
            continue;
        }

        Handle(connectionId);
        shutdown(connectionId, 2);
        close(connectionId);
    }
}
